import { Options, PipelineOperator } from "../../core";
import { Iterator } from "../../lazy";
import { AsyncIterator } from "../../async/lazy";

/**
 * Skips the first n documents where n is the specified skip number and passes the remaining documents unmodified to the pipeline.
 *
 * See {@link https://www.mongodb.com/docs/manual/reference/operator/aggregation/skip usage}.
 *
 * @param collection
 * @param expr
 * @param _options
 * @returns
 */
export const $skip: PipelineOperator = (
  collection: Iterator | AsyncIterator,
  expr: number,
  options: Options
): Iterator | Promise<AsyncIterator> => {
  if (options.isAsync) {
    return Promise.resolve(collection as AsyncIterator).then((collection) => {
      let count = 0;
      return collection.filter(() => {
        return count++ >= expr;
      });
    });
  }
  return (collection as Iterator).drop(expr);
};